package com.yanqu.road.dao.impl.activity.ghost;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.ghost.GhostData;
import com.yanqu.road.entity.activity.ghost.GhostUnionNews;
import com.yanqu.road.entity.activity.ghost.GhostUserAbility;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class GhostUserAbilityDaoImpl extends TempDataObjectDao<GhostUserAbility> {

    public static String selectSql = "select * from t_c_activity_ghost_user_ability where activityId = ?";
    public Map<Long, Map<Long, GhostUserAbility>> getGhostUserAbilityData(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Long, Map<Long, GhostUserAbility>> map = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(selectSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                GhostUserAbility data = parse(rs);
                Map<Long, GhostUserAbility> userMap = map.computeIfAbsent(rs.getLong("serverId"), k -> new ConcurrentHashMap<>());
                userMap.put(rs.getLong("userId"), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    @Override
    protected boolean updateData(GhostUserAbility data) {
        return false;
    }

    public static String insertSql = "INSERT INTO `t_c_activity_ghost_user_ability`(`activityId`, `serverId`, `userId`, `userAbility`) VALUES (?, ?, ?, ?)";
    @Override
    protected boolean addData(GhostUserAbility data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getServerId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserAbility()));
        if (getDb().executeNoneQuery(insertSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected GhostUserAbility parseData(ResultSet rs) throws SQLException {
        GhostUserAbility data = new GhostUserAbility();
        data.setActivityId(rs.getInt("activityId"));
        data.setServerId(rs.getLong("serverId"));
        data.setUserId(rs.getLong("userId"));
        data.setUserAbility(rs.getLong("userAbility"));
        return data;
    }
}
